package com.group.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group.pojo.Delivery;
import com.group.service.DeliveryService;
import com.group.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author group
 * @since 2021-09-06
 */
@RestController
@RequestMapping("/group/delivery")
@Api
public class DeliveryController {

    @Autowired
    private DeliveryService deliveryService;

    @ApiOperation(value = "查询所有")
    @GetMapping("findAll")
    public R findAll(Integer pageNo , Integer pageSize){
        if(pageNo == null){
            pageNo = 1;
        }
        if(pageSize == null){
            pageSize = 100;
        }

        PageHelper.startPage(pageNo,pageSize);
        List<Delivery> list = deliveryService.findAll();
        PageInfo<Delivery> pageInfo = new PageInfo<>(list);
        return R.ok().data("pageinfo",pageInfo);
    }


    @ApiOperation(value = "多条件查询")
    @PostMapping("findByProperty")
    public R findByProperty(@RequestBody Delivery delivery,Integer pageNo){
        Integer pageSize = 5;
        System.out.println(delivery);
        System.out.println(pageNo);
        if(pageNo == null){
            pageNo = 1;
        }
        if(pageSize == null){
            pageSize = 5;
        }

        //订单备注模糊查询
        if(delivery.getDeliverycontent()!=null){
            delivery.setDeliverycontent("%"+delivery.getDeliverycontent()+"%");
        }

        PageHelper.startPage(pageNo,pageSize);
        List<Delivery> list = deliveryService.findByProperty(delivery);
        PageInfo<Delivery> pageInfo = new PageInfo<>(list);
        return R.ok().data("pageinfo",pageInfo);
    }

    @ApiOperation("删除和恢复多订单信息")
    @GetMapping("deleteById")
     public R deleteById(String []args , Integer jud){
        boolean b = deliveryService.deleteByDeliveryId(args,jud);
        return b ? R.ok() : R.error();
    }

    @ApiOperation("审核多订单信息")
    @GetMapping("updateExamineById")
    public R updateExamineById(String []args , Integer jud){
        boolean b = deliveryService.updateDeliveryExamine(args,jud);
        return b ? R.ok() : R.error();
    }

    @ApiOperation("创建订单")
    @PostMapping("insertDelivery")
    public R insertDelivery(@RequestBody Delivery delivery){
        System.out.println(delivery);
        boolean b = deliveryService.insertDelivery(delivery);
        return b ? R.ok() : R.error();
    }

//    @ApiOperation("修改订单信息")
//    @RequestMapping("updateById")
//    public R updateById(Delivery delivery){
//        boolean b = deliveryService.updateDelivery(delivery);
//        return b ? R.ok() : R.error();
//    }

    @ApiOperation(value = "根据id查询所有")
    @GetMapping("findById")
    public R findById(@RequestBody Delivery delivery){
        List<Delivery> list = deliveryService.findById(delivery);
        return R.ok().data("list",list);
    }

    @RequestMapping("findDeliveryById")
    public Delivery findDeliveryById(Integer id){
        Delivery delivery=deliveryService.findDeliveryById(id);
        return delivery;
    }

    @ApiOperation("修改订单信息")
    @PostMapping("updateById")
    public R updateById(@RequestBody Delivery delivery,Integer id){
        System.out.println(delivery);
        boolean b = deliveryService.updateDelivery(delivery,id);
        return b ? R.ok() : R.error();
    }

}

